package com.junzhiit.nengji.cms.web.util;


import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.junzhiit.nengji.cms.web.exception.ImageTypeUnsupportedException;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

public class ThumbnailsUtil {
	
	private final static Log logger = LogFactory.getLog(ThumbnailsUtil.class);
  
    /**
     * 指定大小进行缩放
     * 
     * @throws IOException
     */
	public static void toSize(String sourceFilePath, String targetFilePath,String targetFileName,int width,int height){
        try {
        	File fileFolder = new File(sourceFilePath);
			if (fileFolder.exists()) {
    			fileFolder = new File(targetFilePath);
        		if (!fileFolder.exists()) {
        			fileFolder.mkdirs();
        		}
        		Thumbnails.of(sourceFilePath).size(width, height).toFile(targetFilePath+targetFileName);
    		}else{
    			logger.info("file not found=:"+sourceFilePath);
    		}
			
		} catch (IOException e) {
			logger.error(e,e);
		}
    }
	
	/** 按照比例
	 * @param sourceFilePath 图片源
	 * @param targetFilePath 目标图片路径
	 * @param targetFileName 目标图片名称
	 * @param scale 是可以指定图片的大小，值在0到1之间，1f就是原图大小，0.5就是原图的一半大小，这里的大小是指图片的长宽。
	 * @param outputQuality 是图片的质量，值也是在0到1，越接近于1质量越好，越接近于0质量越差。
	 */
	public static void toScale(String sourceFilePath,String targetFilePath,String targetFileName,float scale,float outputQuality){
		try {
			File fileFolder = new File(sourceFilePath);
			if (fileFolder.exists()) {
    			fileFolder = new File(targetFilePath);
        		if (!fileFolder.exists()) {
        			fileFolder.mkdirs();
        		}
    			Thumbnails.of(sourceFilePath).scale(scale).outputQuality(outputQuality).toFile(targetFilePath+targetFileName);
    		}else{
    			logger.info("file not found=:"+sourceFilePath);
    		}
		   
		} catch (IOException e) {
			throw new ImageTypeUnsupportedException("图片类型不支持");
		}
    }

    /**
     * 旋转
     *  @throws rotate (角度),正数：顺时针 负数：逆时针
     */
    public static void rotate(String sourceFilePath,String targetFilePath,String targetFileName,float rotate,float scale){
    	try {
			File fileFolder = new File(targetFilePath);
    		if (!fileFolder.exists()) {
    			fileFolder.mkdirs();
    		}
    		Thumbnails.of(sourceFilePath).scale(scale).rotate(rotate).toFile(targetFilePath+targetFileName);
		} catch (IOException e) {
			logger.error(e,e);
		}
        
    }
    
    /**
     * 水印
     * 
     * @throws IOException
     */
    public static void watermark(String sourceFilePath,String targetFilePath,String targetFileName,String watermarkImgePath,float scale,float opacity){
        /**
         * watermark(位置，水印图，透明度)
         */
        try {
        	File fileFolder = new File(targetFilePath);
      		if (!fileFolder.exists()) {
      			fileFolder.mkdirs();
      		}
			Thumbnails.of(sourceFilePath).size(370, 370).watermark(Positions.CENTER,ImageIO.read(new File(watermarkImgePath)), opacity).outputQuality(0.8f).toFile(targetFilePath+targetFileName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			logger.error(e, e);
		}
    }

    /**
     * 裁剪
     * 
     * @throws IOException
     */
    public static void cropped(String sourceFilePath,String targetFilePath,String targetFileName,int x, int y, int width, int height,int sizeWidth, int sizeHeight) {
    	
    	  try {
          	File fileFolder = new File(targetFilePath);
      		if (!fileFolder.exists()) {
      			fileFolder.mkdirs();
      		}
            Thumbnails.of(sourceFilePath).sourceRegion(x, y, width, height).size(sizeWidth, sizeHeight).keepAspectRatio(false).toFile(targetFilePath+targetFileName);
      		
  		} catch (IOException e) {
  			logger.error(e, e);
  		}
        
    }

    /**
     * 转化图像格式
     * 
     */
    public static void outputFormat(String sourceFilePath,String targetFilePath,String targetFileName,String format,float scale){
        try {
        	File fileFolder = new File(targetFilePath);
    		if (!fileFolder.exists()) {
    			fileFolder.mkdirs();
    		}
			Thumbnails.of(sourceFilePath).scale(scale).outputFormat(format).toFile(targetFilePath+targetFileName);
		} catch (IOException e) {
			logger.error(e, e);
		}
    }
    
    public  static void main(String[] args){
    	
    	
    	/**
    	 * watermark(位置，水印图，透明度)
    	 */
        try {
        	/*Thumbnails.of("C:\\Users\\Public\\Pictures\\Sample Pictures\\c.jpg").size(320, 320).toFile("C:\\Users\\Public\\Pictures\\Sample Pictures\\cc.jpg");
			Thumbnails.of("C:\\Users\\Public\\Pictures\\Sample Pictures\\white.png").size(370, 370)
			.watermark(Positions.CENTER, ImageIO.read(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\cc.jpg")),1f)
			.outputQuality(1f).toFile("C:\\Users\\Public\\Pictures\\Sample Pictures\\fwh111111.jpg");*/
        	File[] files = new File("E:\\img\\dev_goods\\goods").listFiles();
            for (int i = 1; i < files.length; i++)
            {
                if(files[i].getName().replaceAll("(.jpg|.png|.bmp|.gif)+","").length() != files[i].getName().length())
                {
                	Thumbnails.of("E:\\img\\dev_goods\\goods\\"+files[i].getName()).scale(1f).outputQuality(0.5f).toFile("E:\\img\\dev_goods\\new\\"+files[i].getName());
                    System.out.println(files[i].getName()+"---------->成功第"+i+"条数据------下一条数据----->"+files[i+1].getName());
                }
            }
        	/*Thumbnails.of("D:\\a.png").scale(1f).outputQuality(0.5f).outputFormat("jpg").toFile("D:\\aaaa");*/
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
  
    
}
